extends Control

@onready var vBoxContainer = $ScrollContainer/VBoxContainer
@onready var scrollContainer = $ScrollContainer

var depth = 0
var parentMenuItemName = ""
var containerSize
var commandBox


func init(menuItemsData:Array, _depth = 0, _parentMenuItemName = ""):
	depth = _depth
	parentMenuItemName = _parentMenuItemName
	
	for menuItemData in menuItemsData:
		var inst = Global.createSceneInst(Consts.RESOURCE_UI_POPUP_MENUITEM)
		vBoxContainer.add_child(inst)
		inst.container = self
		inst.init(menuItemData)
		inst.size = Vector2(commandBox.menuItemWidth, commandBox.menuItemHeight)
	
	var tentativeHeight = menuItemsData.size() * commandBox.menuItemHeight + (menuItemsData.size() - 1)
	if tentativeHeight > Global.viewportHeight:
		tentativeHeight = Global.viewportHeight
	scrollContainer.size = Vector2(commandBox.menuItemWidth, tentativeHeight)
	containerSize = scrollContainer.size
		
	
func onMenuItemClick(inst):
	commandBox.onMenuItemClick(inst)
	

func hideAll():
	for index in range(vBoxContainer.get_child_count()):
		var menuItem = vBoxContainer.get_child(index)
		menuItem.hide()
		

func showAll():
	for index in range(vBoxContainer.get_child_count()):
		var menuItem = vBoxContainer.get_child(index)
		menuItem.show()
	
